package Midium;

import java.util.Stack;

//19. 删除链表的倒数第N个节点
public class Solution19 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        Stack<ListNode> stack = new Stack<ListNode>();
        ListNode p = head;
        int count = 0;
        while (p!=null){
            stack.push(p);
            p=p.next;
            count++;
        }
        if(n==count)
            return head.next;
        ListNode pre,post=null;
        for(int i = 0 ; i < n ; i++){
            post = stack.pop();
        }
        pre = stack.peek();
        pre.next=post.next;
        return head;
    }
}
